/*
 *  WeaponObject.cpp
 *  Rempart
 *
 *  Created by julien BONACHERA on 03/06/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "WeaponObject.h"

WeaponObject::WeaponObject(){
	this->bulletList = new Index<Bullet*>();
	this->localClock = new sf::Clock();
	this->loop = true;
	this->enabled = true;
	this->Launch();
}
WeaponObject::~WeaponObject(){
	this->loop = false;
	this->Wait();
	this->bulletList->deleteAllObjects();
	delete this->bulletList;
	delete this->localClock;
}


void WeaponObject::manageBullets() const{
	if (this->bulletList->size() > 0) {
		for (int i = this->bulletList->size() - 1; i >= 0 ; i--) {
			this->bulletList->getObjectAt(i)->move();
		}
	}
	
	
}

void WeaponObject::fire(const sf::Vector2<float>& target){
	sf::Lock Lock(globalLock);
	if(this->enabled && this->localClock->GetElapsedTime() > 1.0){
		this->localClock->Reset();
		this->bulletList->addObject(new Bullet(target, this->GetPosition(), (this->elementType == MapElement::boat)));
	}
}



int WeaponObject::getNumberOfBullets(){
	return this->bulletList->size();
}

Bullet* WeaponObject::getBulletAt(int i){ 
	return this->bulletList->getObjectAt(i);
}

void WeaponObject::deleteBulletAt(int i){
	this->bulletList->deleteObjectAt(i);
}

void WeaponObject::Run(){
	while (this->loop) {
		this->manageBullets();
		sf::Sleep(0.01);
	}
	
}

void WeaponObject::disable(){
	this->unsetVisible();
	this->elementType = MapElement::killedBoat;
	this->enabled = false;
}


